<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%String path = request.getContextPath();%>
<script type="text/javascript">
var curCardEntity;
function changeUpgradeType(type){
	if (type==0) {
		$("#p_reduce").css("display", "none");
		$("#p_upgrade").css("display", "block");
		$("#sel_upgrade").find("option[value='"+curCardEntity.id+"']").attr("selected", true);
	}
	else {
		$("#p_upgrade").css("display", "none");
		$("#p_reduce").css("display", "block");
		$("#sel_reduce").find("option[value='"+curCardEntity.id+"']").attr("selected", true);
	}
}
function showLevel(json) {
	var allLevelArr=new Array(),upgradeStr,reduceStr;
	var curCardId="${param.cardId}";
  	for (var i in json) {
  		allLevelArr.push(json[i]);
  		if (curCardId==json[i].id) {
  			curCardEntity = json[i];
  			$("#levelNameId").html(curCardEntity.cardName);
  			$("#levelId").html(curCardEntity.cardLevelVO.levelName+"（"+curCardEntity.cardLevelVO.level+"）");
  		}
  	}
  	allLevelArr = allLevelArr.sort(function(a,b){
  		return a.cardLevelVO.level-b.cardLevelVO.level;
  	});
  	for (var i in allLevelArr) {
  		if (allLevelArr[i].cardLevelVO.level < curCardEntity.cardLevelVO.level) continue;
  		var textStr = allLevelArr[i].cardName+"【"+allLevelArr[i].cardLevelVO.levelName+"（"+allLevelArr[i].cardLevelVO.level+"）】";
  		upgradeStr += "<option value='"+allLevelArr[i].id+"'>"+textStr+"</option>";
  	}
  	for (var i=(allLevelArr.length-1); i > -1; i--) {
  		if (allLevelArr[i].cardLevelVO.level > curCardEntity.cardLevelVO.level) continue;
  		var textStr = allLevelArr[i].cardName+"【"+allLevelArr[i].cardLevelVO.levelName+"（"+allLevelArr[i].cardLevelVO.level+"）】";
  		reduceStr += "<option value='"+allLevelArr[i].id+"'>"+textStr+"</option>";
  	}
  	$(upgradeStr).appendTo($("#sel_upgrade"));
  	$(reduceStr).appendTo($("#sel_reduce"));
  	changeUpgradeType(0);
}
function subUpgradeForm(obj) {
	if($("input[name='upgradeType']:checked").val()==0)
		$("input[name='memberCardVO.cardId']").val($("#sel_upgrade").find("option:selected").val());
	else
		$("input[name='memberCardVO.cardId']").val($("#sel_reduce").find("option:selected").val());
	//没有更换会员卡
	if (curCardEntity.id == $("input[name='memberCardVO.cardId']").val()) {
		processHandleResult(null);
		return false;
	}
	return iframeCallback(obj, processHandleResult);
}
function processHandleResult(json){
	parent.reloadPage();
	$.pdialog.closeCurrent();
}
</script>
<div class="pageContent">
	<form action="<%=path%>/memberCard/upgradeMemberCard.action" method="post" class="pageForm" onsubmit="return subUpgradeForm(this);">
	<input type="hidden" name="memberCardVO.id" value="${param.id}">
	<input type="hidden" name="memberCardVO.cardId">
	<div class="pageFormContent" layoutH="56">
		<p style="width:100%;float: none;text-align: center"></p>
		<p style="width:100%;float: none;text-align: center">
			当前会员卡：<b id="levelNameId"></b> 级别为：<b id="levelId"></b>
		</p>
		<div class="divider"></div>
		<p style="width:100%;float: none;text-align: center">
			<input type="radio" name="upgradeType" value="0" checked="checked" onclick="changeUpgradeType(this.value);"/>升级会员卡
			<input type="radio" name="upgradeType" value="1" onclick="changeUpgradeType(this.value);"/>降级会员卡
		</p>
		<div class="divider" style="border: 0"></div>
		<p style="width:100%;float: none;display: none;text-align: center" id="p_upgrade">
			<select id="sel_upgrade" style="float: none"></select>
		</p>
		<div class="divider" style="border: 0"></div>
		<p style="width:100%;float: none;display: none;text-align: center" id="p_reduce">
			<select id="sel_reduce" style="float: none"></select>
		</p>
	</div>
	<div class="formBar">
		<ul>
			<li><div class="buttonActive"><div class="buttonContent"><button id="upload_file_button" type="submit">保存</button></div></div></li>
			<li><div class="button"><div class="buttonContent"><button class="close" type="button">取消</button></div></div></li>
		</ul>
	</div>
	</form>
</div>
<script type="text/javascript">$.ajax({cache:false, url:"${pageContext.request.contextPath}/ajax/ajaxGetCardEntityByCustomer.do", type:"post", dataType:"json", data:{customerId:${param.customerId}}, success:showLevel});</script>